package com.shell.governor.srvhall.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.srvhall.po.PubSrvHallItem;
import com.shell.governor.srvhall.service.PubSrvHallItemService;
import com.shell.governor.srvhall.service.PubSrvHallService;
import com.shell.governor.srvhall.vo.SrvHallCategoryVo;
import com.shell.governor.srvhall.vo.SrvHallItemVo;

@RestController
public class SvrhallController {
	@Autowired
	private PubSrvHallItemService pubSrvHallItemService;
	@Autowired
	private PubSrvHallService pubSrvHallService;

	@RequestMapping
	@Logging(module = "��������", methods = "����������������")
	public List<SrvHallItemVo> getSrvhallItemByPage(PageLimit limit,
			@RequestParam(name = "itemCategory", required = false) String itemCategory,
			@RequestParam(name = "hot", required = false) String hot,
			@RequestParam(name = "searchStr", required = false) String searchStr) {
		return this.pubSrvHallItemService.getSrvHallItemByPage(limit, itemCategory, hot, searchStr);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������������")
	public List<SrvHallCategoryVo> getSrvhallCat(@RequestParam(name = "catId", required = false) String catId) {
		return this.pubSrvHallService.getSrvhallCat(catId);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������������")
	public List<SrvHallItemVo> getSrvhallItemByCategory(
			@RequestParam(name = "searchStr", required = false) String searchStr,
			@RequestParam(name = "subCat", required = false) String subCat) {
		PageLimit limit = new PageLimit();
		limit.setDoPage(false);
		return getSrvhallItemByPage(limit, subCat, null, searchStr);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������������")
	public RetMsg delSrvhallItem(@RequestParam(name = "itemId") String itemId) {
		int rs = this.pubSrvHallItemService.delete(itemId);
		if (rs > 0) {
			return new RetMsg("success", true, "����������");
		}
		return new RetMsg("warning", true, "����������������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public Map<String, Object> uploadFile(@RequestParam(value = "file", required = false) MultipartFile file,
			String fileType, String savePath) throws Exception {
		return this.pubSrvHallService.uploadFile(file, fileType, savePath);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������������")
	public Integer getHotNum() {
		PubSrvHallItem item = new PubSrvHallItem();
		item.setHot("1");
		List<PubSrvHallItem> hallItems = this.pubSrvHallItemService.selectByEntity(item);
		if (null != hallItems) {
			return Integer.valueOf(hallItems.size());
		}
		return Integer.valueOf(0);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public List<SrvHallItemVo> getHotSrvhallItem(@RequestParam(name = "searchStr", required = false) String searchStr) {
		PageLimit limit = new PageLimit(1, 6);
		List<SrvHallItemVo> items = getSrvhallItemByPage(limit, null, null, searchStr);
		return items;
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������������")
	public RetMsg saveSrvhallItem(SrvHallItemVo vo) {
		if (StringUtil.empty(vo.getItemId())) {
			vo.setItemId(SystemUtil.getUUID());
			vo.setCreateTime(getCurTime());
			vo.setCreatorId(CommonDataUtil.getCurUser().getUserId());
		}
		vo.setEditTime(getCurTime());
		vo.setEditorId(CommonDataUtil.getCurUser().getUserId());
		this.pubSrvHallItemService.saveOrUpdateItem(vo);
		return new RetMsg("success", true, "����������");
	}

	private String getCurTime() {
		String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		return currentTime;
	}
}
